import {login ,logout,getInfo} from '../../api/login';
import Cookies from 'js-cookie';
const user = {
    state: {
        user: '',
        status: '',
        code: '',
        token: '',
        name: '',
        avatar: '',
        introduction: '',
        roles: [],
        setting: {
            articlePlatform: []
        }
    },

    mutations: {
        SET_CODE: (state, code) => {
            state.code = code;
        },
        SET_TOKEN: (state, token) => {
            state.token = token;
        },
        SET_INTRODUCTION: (state, introduction) => {
            state.introduction = introduction;
        },
        SET_SETTING: (state, setting) => {
            state.setting = setting;
        },
        SET_STATUS: (state, status) => {
            state.status = status;
        },
        SET_NAME: (state, name) => {
            state.name = name;
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar;
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles;
        }
    },

    actions: {
        // 用户名登录
        loginByUserInfo({ commit }, userInfo) {
            console.log(userInfo);
            return new Promise((resolve, reject) => {
                login(userInfo.username,userInfo.password).then(response => {
                    const data = response.data;
                    if(data.success){
                        commit('SET_TOKEN', data.token);
                    }else{

                    }
                    resolve(data);
                }).catch(error => {
                    reject(error);
                });
            });
        }
    }
}

export default user